package GUI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.RowSorter;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import dao.MoistureDao;
import dao.equipmentDao;
import dao.hatchwayDao;
import dao.modelDao;
import dao.senseDeviceDao;

import entity.Equipment;
import entity.Hatchway;
import entity.Model;
import entity.SenseDevice;
import entity.Moisture;
//
////所有的配置信息页面
public class equipmentGUI extends JFrame {
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;//显示信息面板
	static DefaultTableModel defaultModel, defaultModel1, defaultModel2, defaultModel3, defaultModel4, defaultModel5;//
	final JPanel equipPanel = new JPanel();
	JPanel menu = new JPanel();
	final JPanel modelPanel = new JPanel();
	final JPanel devicePanel = new JPanel();
	final JPanel rtuPanel = new JPanel();
	JPanel tcpPanel = new JPanel();
	JPanel moisturePanel = new JPanel();//墒情采集
	JPanel testsh = new JPanel();
//	static int[] buttonst = new int[36];
	private static JTable table, table_1, table_2, table_3, table_4,table_5;
//	private static JTextArea show;
//	int comnum = 7;

	public equipmentGUI() {
		
	    setTitle("设备配置信息");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(20, 20, 1000, 700);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		Peql();  //通道信息
		Pmenu(); //菜单
		Pmodel();//模块
		PsenceDevice();//设备
		Prtu();
		Ptcp();
		PMoisture();
//		Ptestsh();
		modelPanel.setVisible(false);
		devicePanel.setVisible(false);
		rtuPanel.setVisible(false);
		tcpPanel.setVisible(false);
		moisturePanel.setVisible(false);
		testsh.setVisible(false);

	}
	/********************************所有设备**************************************/
	private void Peql() {

		equipPanel.setBounds(0, 0, 984, 590);
		equipPanel.setLayout(null);

		String[] name = { "通道id", "通道名称", "通道类型", "ip地址","端口号","节点id", "节点名称", "节点地址","设备id","设备名称","设备类型","寄存器地址"};
		
		String[][] data = new String[0][0];
		defaultModel = new DefaultTableModel(data, name);
		table = new JTable(defaultModel);
		table.setPreferredScrollableViewportSize(new Dimension(700, 60));
		JScrollPane s = new JScrollPane(table);
		s.setBounds(10, 10, 975, 490);
		s.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		table.setToolTipText("设备列表");
		table.setBounds(0, 10, 733, 208);
		equipPanel.add(s);
		contentPane.add(equipPanel);

		JButton btnNewButton = new JButton("\u5237\u65B0\u5217\u8868");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				equipmentDao.EquipmentInformation();
//				code_classify.fresh();
				
				try {
					RowSorter<?> rowSort = table.getRowSorter();
					if (rowSort != null) {
						rowSort.setSortKeys(null);// 如果使用了排序器，先清除对表的排序
					}
					((DefaultTableModel) table.getModel()).getDataVector().clear();
					((DefaultTableModel) table.getModel()).fireTableDataChanged();
					table.updateUI();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				addTable_eql();
			}
		});
		btnNewButton.setBounds(868, 519, 93, 23);
		equipPanel.add(btnNewButton);
		addTable_eql();
	}

	
	public static void addTable_eql() {		
		List<Equipment> list_all_equ = equipmentDao.readAll();
		
		for (int i = 0; i < list_all_equ.size(); i++) {  //遍历通道
			Equipment s = new Equipment();
			s = (Equipment) list_all_equ.get(i);   //显示通道信息
			@SuppressWarnings("rawtypes")
			Vector<Comparable> v = new Vector<Comparable>();
			v.add(s.getId());
			v.add(s.getHatchway_name());
			v.add(s.getHatchway_type());
//			v.add(s.getBaud_rate());
//			v.add(s.getOdd_even_check());
//			v.add(s.getData_bits());
//			v.add(s.getStream_control());
//			v.add(s.getStop_bit());
			v.add(s.getIp_address());
			v.add(s.getIp_port());
			
			v.add(s.getModel_id());
			v.add(s.getModel_name());
			//v.add(s.getHatchway_name());
//			v.add(s.getHatchway_id());
			v.add(s.getModel_address());
//			v.add(s.getRund_time());
//			v.add(s.getNote());
			
			v.add(s.getDevice_id());
			v.add(s.getDevice_name());
			v.add(s.getDevice_type());
			v.add(s.getRegister_address());
			
			defaultModel.addRow(v);
		}				
	}
	
/**********************************模块（节点）************************************************/	
	private void Pmodel() {

		modelPanel.setBounds(0, 0, 984, 590);
		modelPanel.setLayout(null);
		String[] name = { "id","节点名称", "所属通道id", "节点地址", "采样周期","备注" };
		String[][] data1 = new String[0][0];
		defaultModel1 = new DefaultTableModel(data1, name);
		table_1 = new JTable(defaultModel1);
		table_1.setPreferredScrollableViewportSize(new Dimension(700, 60));
		JScrollPane s = new JScrollPane(table_1);
		s.setBounds(10, 10, 974, 316);
		s.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		table_1.setToolTipText("模块列表");
		table_1.setBounds(0, 10, 733, 208);
		modelPanel.add(s);
		contentPane.add(modelPanel);
		JButton btnNewButton = new JButton("刷新列表");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				modelDao.ModelInformation();
//				code_classify.fresh();
				try {
					RowSorter<?> rowSort = table_1.getRowSorter();
					if (rowSort != null) {
						rowSort.setSortKeys(null);// 如果使用了排序器，先清除对表的排序
					}
					((DefaultTableModel) table_1.getModel()).getDataVector().clear();
					((DefaultTableModel) table_1.getModel()).fireTableDataChanged();
					table_1.updateUI();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				addTable_model();
			}
		});
		btnNewButton.setBounds(868, 519, 93, 23);
		modelPanel.add(btnNewButton);
		addTable_model();
		
	}
			
	public static void addTable_model() {	
		
		List<Model> list_all=modelDao.ModelInformation();
	
	for (int i = 0; i < list_all.size(); i++) {  //遍历通道	
			Model m = new Model();
			m = (Model) list_all.get(i);   //模块信息
			@SuppressWarnings("rawtypes")
			Vector<Comparable> v = new Vector<Comparable>();
			v.add(m.getId());
			v.add(m.getModel_name());
			//v.add(m.getHatchway_name());
			v.add(m.getHatchway_id());
			v.add(m.getModel_address());
			v.add(m.getRund_time());
			v.add(m.getNote());
			defaultModel1.addRow(v);
		}				
	}
	/********************************设备*******************************************/
	private void PsenceDevice() {//设备

		devicePanel.setBounds(0, 0, 984, 590);
		devicePanel.setLayout(null);
		String[] name = { "id","设备名称", "设备类型", "寄存器地址", "节点地址", "设备编号","状态","经度","纬度","原始最大值","原始最小值","实际最大值","实际最小值"};
		String[][] data = new String[0][0];
		defaultModel2 = new DefaultTableModel(data, name);
		table_2 = new JTable(defaultModel2);
		table_2.setPreferredScrollableViewportSize(new Dimension(700, 60));
		JScrollPane s = new JScrollPane(table_2);
		s.setBounds(10, 10, 975, 490);
		s.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		table_2.setToolTipText("设备管理");
		table_2.setBounds(0, 10, 733, 208);
		devicePanel.add(s);
		contentPane.add(devicePanel);

		JButton btnNewButton = new JButton("\u5237\u65B0\u5217\u8868");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				senseDeviceDao.SenseDevicelInformation();
//				code_classify.fresh();
				try {
					RowSorter<?> rowSort = table_2.getRowSorter();
					if (rowSort != null) {
						rowSort.setSortKeys(null);// 如果使用了排序器，先清除对表的排序
					}
					((DefaultTableModel) table_2.getModel()).getDataVector().clear();
					((DefaultTableModel) table_2.getModel()).fireTableDataChanged();
					table_2.updateUI();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				addTable_senceDevice();
			}
		});
		btnNewButton.setBounds(868, 519, 93, 23);
		devicePanel.add(btnNewButton);
		addTable_senceDevice();
		
	}
	public static void addTable_senceDevice() {	
		
		List<SenseDevice> list_all=senseDeviceDao.SenseDevicelInformation();
	
	for (int i = 0; i < list_all.size(); i++) {  //遍历通道
		SenseDevice m = new SenseDevice();
			m = (SenseDevice) list_all.get(i);   //模块信息
			@SuppressWarnings("rawtypes")
			Vector<Comparable> v = new Vector<Comparable>();
			v.add(m.getId());
			v.add(m.getDevice_name());
			v.add(m.getDevice_type());
			v.add(m.getRegister_address());
			v.add(m.getNode_address());
			v.add(m.getDevice_number());
			v.add(m.getDevice_status());
			v.add(m.getLng());
			v.add(m.getLat());
			v.add(m.getOriginal_maxvalue());
			v.add(m.getOriginal_minvalue());
			v.add(m.getReal_maxvalue());
			v.add(m.getReal_minvalue());
			v.add(m.getNode_address());
//			v.add(m.getDevice_icon());
//			v.add(m.getNote());
			defaultModel2.addRow(v);
		}				
	}
/************************************rtu**********************************************/
	private void Prtu() {

		rtuPanel.setBounds(0, 0, 984, 590);
		rtuPanel.setLayout(null);
		String[] name = { "id", "通道名称", "流控制", "通道类型","波特率","数据位","奇偶校验","停止位"};
		String[][] data = new String[0][0];
		defaultModel3 = new DefaultTableModel(data, name);
		table_3 = new JTable(defaultModel3);
		table_3.setPreferredScrollableViewportSize(new Dimension(700, 60));
		JScrollPane s = new JScrollPane(table_3);
		s.setBounds(10, 10, 974, 316);
		s.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		table_3.setToolTipText("开关状态列表");
		table_3.setBounds(0, 10, 733, 208);
		rtuPanel.add(s);
		contentPane.add(rtuPanel);
		JButton btnNewButton = new JButton("刷新列表");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				senseDeviceDao.SenseDevicelInformation();
//				code_classify.fresh();
				try {
					RowSorter<?> rowSort = table_3.getRowSorter();
					if (rowSort != null) {
						rowSort.setSortKeys(null);// 如果使用了排序器，先清除对表的排序
					}
					((DefaultTableModel) table_3.getModel()).getDataVector().clear();
					((DefaultTableModel) table_3.getModel()).fireTableDataChanged();
					table_3.updateUI();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				addTable_Rtu();
			}
		});
		btnNewButton.setBounds(868, 519, 93, 23);
		rtuPanel.add(btnNewButton);
		addTable_Rtu();
	}
public static void addTable_Rtu() {	
		
		List<Hatchway> list_all=hatchwayDao.rtuInformation();
	
	for (int i = 0; i < list_all.size(); i++) {  //遍历通道
		Hatchway m = new Hatchway();
			m = (Hatchway) list_all.get(i);   //模块信息
			@SuppressWarnings("rawtypes")
			Vector<Comparable> v = new Vector<Comparable>();
			v.add(m.getId());
			v.add(m.getHatchway_name());
			v.add(m.getStream_control());
			v.add(m.getHatchway_type());
			v.add(m.getBaud_rate());
			v.add(m.getData_bits());
			v.add(m.getOdd_even_check());
			v.add(m.getStop_bit());

			defaultModel3.addRow(v);
		}				
	}
/******************************TCP***************************************************/
private void Ptcp() {

	tcpPanel.setBounds(0, 0, 984, 590);
	tcpPanel.setLayout(null);
	String[] name = { "id", "通道名称",  "通道类型","IP地址","端口号"};
	String[][] data = new String[0][0];
	defaultModel4 = new DefaultTableModel(data, name);
	table_4 = new JTable(defaultModel4);
	table_4.setPreferredScrollableViewportSize(new Dimension(700, 60));
	JScrollPane s = new JScrollPane(table_4);
	s.setBounds(10, 10, 974, 316);
	s.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	table_4.setToolTipText("开关状态列表");
	table_4.setBounds(0, 10, 733, 208);
	tcpPanel.add(s);
	contentPane.add(tcpPanel);
	JButton btnNewButton = new JButton("刷新列表");
	btnNewButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			senseDeviceDao.SenseDevicelInformation();
//			code_classify.fresh();
			try {
				RowSorter<?> rowSort = table_4.getRowSorter();
				if (rowSort != null) {
					rowSort.setSortKeys(null);// 如果使用了排序器，先清除对表的排序
				}
				((DefaultTableModel) table_4.getModel()).getDataVector().clear();
				((DefaultTableModel) table_4.getModel()).fireTableDataChanged();
				table_4.updateUI();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			addTable_Tcp();
		}
	});
	btnNewButton.setBounds(868, 519, 93, 23);
	tcpPanel.add(btnNewButton);
	addTable_Tcp();
}
public static void addTable_Tcp() {	
	
	List<Hatchway> list_all=hatchwayDao.tcpInformation();


for (int i = 0; i < list_all.size(); i++) {  //遍历通道
	Hatchway m = new Hatchway();
		m = (Hatchway) list_all.get(i);   //模块信息
		@SuppressWarnings("rawtypes")
		Vector<Comparable> v = new Vector<Comparable>();
		v.add(m.getId());
		v.add(m.getHatchway_name());
		v.add(m.getHatchway_type());
		v.add(m.getIp_address());
		v.add(m.getIp_port());

		defaultModel4.addRow(v);
	}				
}
/******************************土壤墒情采集***************************************************/
private void PMoisture() {

	moisturePanel.setBounds(0, 0, 984, 590);
	moisturePanel.setLayout(null);
	String[] name = {"设备id","value","采集日期"};
	//String[] name = { "id"};
	String[][] data = new String[0][0];
	defaultModel5 = new DefaultTableModel(data, name);
	table_5 = new JTable(defaultModel5);
	table_5.setPreferredScrollableViewportSize(new Dimension(700, 60));
	JScrollPane s = new JScrollPane(table_5);
	s.setBounds(10, 10, 974, 490);//改变窗口大小
	s.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	table_5.setToolTipText("开关状态列表");
	table_5.setBounds(0, 10, 733, 208);
	moisturePanel.add(s);
	contentPane.add(moisturePanel);
	JButton btnNewButton = new JButton("刷新列表");
	btnNewButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
	//		MoistureDao.moistureInformation();
//			code_classify.fresh();
			try {
				RowSorter<?> rowSort = table_5.getRowSorter();
				if (rowSort != null) {
					rowSort.setSortKeys(null);// 如果使用了排序器，先清除对表的排序
				}
				((DefaultTableModel) table_5.getModel()).getDataVector().clear();
				((DefaultTableModel) table_5.getModel()).fireTableDataChanged();
				table_5.updateUI();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
//			addTable_Moisture();
		}
	});
	btnNewButton.setBounds(868, 519, 93, 23);
	moisturePanel.add(btnNewButton);
//	addTable_Moisture(Moisture);
}
public static void addTable_Moisture(Moisture m) {	
	
//	List<Moisture> list_all=MoistureDao.readAll();

//	for (int i = 0; i < list_all.size(); i++) {  //遍历通道
//		int row=i+1;
	//	Moisture m = new Moisture();
	//	m = (Moisture) list_all.get(i);   //模块信息
		@SuppressWarnings("rawtypes")
		Vector<Comparable> v = new Vector<Comparable>();
//		v.add(m.getId());
//		v.add(row);
		v.add(m.getDevice_id());
		v.add(m.getValue());
		v.add(m.getCreate_date());
		defaultModel5.addRow(v);
//	}				
}
/*******************************菜单************************************************/
	private void Pmenu() {
		menu.setBackground(new Color(135, 206, 250));
		menu.setBounds(0, 589, 984, 73);
		contentPane.add(menu);
		menu.setLayout(null);
		final JButton eqlButton = new JButton("设备列表");
		eqlButton.setBounds(10, 10, 117, 37);
		menu.add(eqlButton);
		final JButton modelButton = new JButton("模块列表");
		modelButton.setBounds(150, 10, 117, 37);
		menu.add(modelButton);
		final JButton deviceButton = new JButton("设备管理");
		deviceButton.setBounds(300, 10, 117, 37);
		menu.add(deviceButton);

		final JButton rtuButton = new JButton("rtu信息");
		rtuButton.setBounds(450, 10, 117, 37);
		menu.add(rtuButton);

		final JButton tcpButton = new JButton("Tcp信息");
		tcpButton.setBounds(600, 10, 117, 37);
		menu.add(tcpButton);
		
		final JButton moistureButton = new JButton("墒情采集");
		moistureButton.setBounds(750, 10, 117, 37);
		menu.add(moistureButton);
//		
//		final JButton stest = new JButton("\u6D4B\u8BD5\u547D\u4EE4\u663E\u793A");
//		stest.setBounds(881, 13, 93, 34);
//		menu.add(stest);
		ActionListener Lmenu = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == eqlButton) {//显示通道
					modelPanel.setVisible(false);
					devicePanel.setVisible(false);
					rtuPanel.setVisible(false);
					tcpPanel.setVisible(false);
					moisturePanel.setVisible(false);
					testsh.setVisible(false);
					equipPanel.setVisible(true);

				} else if (e.getSource() == modelButton) {//
					equipPanel.setVisible(false);
					devicePanel.setVisible(false);
					rtuPanel.setVisible(false);
					tcpPanel.setVisible(false);
					moisturePanel.setVisible(false);
					testsh.setVisible(false);
					modelPanel.setVisible(true);

				} 
				else if (e.getSource() == deviceButton) {//显示设备
					modelPanel.setVisible(false);
					equipPanel.setVisible(false);
					rtuPanel.setVisible(false);
					tcpPanel.setVisible(false);
					moisturePanel.setVisible(false);
					testsh.setVisible(false);
					devicePanel.setVisible(true);

				} 
				else if (e.getSource() == rtuButton) {
					modelPanel.setVisible(false);
					equipPanel.setVisible(false);
					devicePanel.setVisible(false);
					tcpPanel.setVisible(false);
					moisturePanel.setVisible(false);
					testsh.setVisible(false);
					rtuPanel.setVisible(true);

				} 
				else if (e.getSource() == tcpButton) {
					modelPanel.setVisible(false);
					equipPanel.setVisible(false);
					devicePanel.setVisible(false);
					rtuPanel.setVisible(false);
					testsh.setVisible(false);
					tcpPanel.setVisible(true);
					moisturePanel.setVisible(false);
				}
				else if (e.getSource() == moistureButton) {
					modelPanel.setVisible(false);
					equipPanel.setVisible(false);
					devicePanel.setVisible(false);
					rtuPanel.setVisible(false);
					testsh.setVisible(false);
					tcpPanel.setVisible(false);
					moisturePanel.setVisible(true);
				}
//				else if (e.getSource() == stest) {
//					modelPanel.setVisible(false);
//					equipPanel.setVisible(false);
//					devicePanel.setVisible(false);
//					rtuPanel.setVisible(false);
//				moisturePanel.setVisible(false);
//					tes.setVisible(false);
//					testsh.setVisible(true);
//				}
			}
		};
		eqlButton.addActionListener(Lmenu);
		modelButton.addActionListener(Lmenu);
		deviceButton.addActionListener(Lmenu);
		rtuButton.addActionListener(Lmenu);
		tcpButton.addActionListener(Lmenu);
		moistureButton.addActionListener(Lmenu);
//		stest.addActionListener(Lmenu);

	}




}
